package cn.lhl01.task;

import cn.lhl01.outside_interfac.IUnifiedIdentity;
import cn.lhl01.sys.entity.po.SysMenu;
import cn.lhl01.sys.entity.po.SysUser;
import cn.lhl01.sys.service.impl.SysMenuServiceImp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
@Slf4j
@RequiredArgsConstructor
public class TaskDemo {

	@NonNull
	private RedisTemplate redisTemplate;

//	@NonNull
//	private ResourcePatternResolver resourcePatternResolver;

	@NonNull
	private SysMenuServiceImp sysMenuServiceImp;

	@NonNull
	private WebApplicationContext applicationContext;
	@NonNull
	private IUnifiedIdentity unifiedIdentity;

	/**
	 * 项目启动完成 执行一次
	 * 保存项目中的url资源到数据库
	 */
	@PostConstruct
	public void synTask3() {

		RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
		//获取url与类和方法的对应信息
		Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
		List<String> urlList = new ArrayList<>();
		for (RequestMappingInfo info : map.keySet()) {
			//获取url的Set集合，一个方法可能对应多个url
			Set<String> patterns = info.getPatternValues();
			for (String url : patterns) {
				urlList.add(url);
			}
		}
		System.out.println(urlList);

		for (String url : urlList) {
			LambdaQueryWrapper<SysMenu> wrapper = Wrappers.<SysMenu>lambdaQuery();
			wrapper.eq(SysMenu::getUrl, url); //url唯一

			SysMenu sysMenu = new SysMenu();
			sysMenu.setUrl(url); //url
			sysMenu.setAuthority(url); //权限标识
			sysMenuServiceImp.saveOrUpdate(sysMenu, wrapper);
		}
	}

	//@Scheduled(cron = "0/5 * * * * *")
	public void synTask() {


		SysUser user = new SysUser();
		user.setId(100L);
		user.setUsername("彭于晏");
		//使用redisTemplate保存数据
		redisTemplate.opsForValue().set("user", user);
		log.info("log_________");

	}

	//@Scheduled(cron = "0/20 * * * * *")
	public void synTask2() {
		//使用redisTemplate 取数据
		SysUser user1 = (SysUser) redisTemplate.opsForValue().get("user");
		System.out.println(user1);
		log.info("log_________" + user1);

	}

	//@Scheduled(cron = "0/20 * * * * *")
	public void synTask4() {
		Object user = unifiedIdentity.getLocation("121.475078", "31.223577");
		log.info("log_________" + user);
	}

	//	/@Scheduled(cron = "0/20 * * * * *")
	public void synTask5() {
		Object user = unifiedIdentity.getUserInfo("", "",
				(e, forestRequest, forestResponse) -> {

					System.out.println(123);
					//啥也不干 返回null

					throw new RuntimeException("");
					//抛出异常 对应异常点在调用行 可以做全局异常处理

				}
		);
		log.info("log_________" + user);
	}


}
